<?php
/*
 module:		红娘管理->红娘付费设置
 create_time:	2024-03-22 16:27:06
 author:    937658938@qq.com
 contact:
*/

namespace app\backend\controller;
use app\backend\model\ZhMatchmakerCharge as ZhMatchmakerChargeModel;
use app\backend\service\ZhMatchmakerChargeService;
use app\backend\service\ZhMatchmakerLevelService;

class ZhMatchmakerCharge extends Admin
{
    /*首页数据列表*/
    function index()
    {
        if (!$this->request->isAjax()) {
            return view('index');
        } else {
            $limit           = $this->request->post('limit', 20, 'intval');
            $offset          = $this->request->post('offset', 0, 'intval');
            $where['entry_name']   = $this->request->param('entry_name', '', 'serach_in');
            $where['is_fixed_price'] = $this->request->param('is_fixed_price', '', 'serach_in');

            $page = floor($offset / $limit) + 1;
            $res  = ZhMatchmakerChargeService::indexList(formatWhere($where), $limit, $page);
            return json($res);
        }
    }

    /*修改*/
    function update()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get('id', '', 'serach_in');
            if (!$id) $this->error('参数错误');
            //查询数据
            $this->view->assign('info', checkData(ZhMatchmakerChargeModel::find($id)));
            //查询红娘等级列表
            $res = ZhMatchmakerLevelService::indexList(formatWhere(['status' => 1]), 100, 1);
            $this->view->assign('level_rows', $res['rows']);
            return view('update');
        } else {
            $postField          = 'id,entry_name,comment,is_fixed_price,matchmaker_money_json';
            $data      = $this->request->only(explode(',', $postField), 'post', null);
            $res = ZhMatchmakerChargeService::update($data);
            return json(['status' => '00', 'msg' => '修改成功','res'=>$res]);
        }
    }

    /**
     * @return \think\response\Json|\think\response\View
     * @author  lwx
     */
    function add()
    {
        if (!$this->request->isPost()) {
            $res = ZhMatchmakerLevelService::indexList(formatWhere([
                'status' => 1
            ]), 100, 1);
            $this->view->assign('level_rows', $res['rows']);
            return view('add');
        } else {
            $postField          = 'entry_name,comment,is_fixed_price,matchmaker_money_json';
            $data               = $this->request->only(explode(',', $postField), 'post', null);
            $data['createtime'] = time();
            $res                = ZhMatchmakerChargeModel::create($data);
            return json(['status' => '00', 'msg' => '添加成功','req'=>$data]);
        }
    }

    /*删除*/
    function delete()
    {
        $idx = $this->request->post('id', '', 'serach_in');
        if (!$idx) $this->error('参数错误');
        try {
            ZhMatchmakerChargeModel::destroy(['id' => explode(',', $idx)], true);
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return json(['status' => '00', 'msg' => '操作成功']);
    }


}

?>